package com.atguigu.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author Fxz
 * @version 1.0
 * @date 2022-02-05 14:07
 */
@Configuration
public class CorsConfig {

	@Bean
	public CorsWebFilter corsWebFilter() {
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		// get post put表示任意
		corsConfiguration.addAllowedMethod("*");
		// 允许任意域名
		corsConfiguration.addAllowedOrigin("*");
		// 任意请求头
		corsConfiguration.addAllowedHeader("*");
		// 允许携带cookie
		corsConfiguration.setAllowCredentials(true);

		UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
		corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);

		return new CorsWebFilter(corsConfigurationSource);
	}

}
